<?php

namespace App\Console\Commands;

use App\Models\DealBuyer;
use App\Models\DealLine as Deal_line;
use App\Models\EDealLog;
use Illuminate\Console\Command;

class DealLine extends Command
{

    protected $signature = 'timechain:deal_line';

    protected $description = '生成每日E币价格';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        //24小时内
        $start_time = date("Y-m-d", strtotime("-1 day"));

        $yesterday = Deal_line::orderBy('id', 'desc')->first();
        if ($yesterday) {
            $yesterday_price = $yesterday->price;
        } else {
            $yesterday_price = 1;
        }
        
        $deal_price = DealBuyer::whereDate('created_at', $start_time)->orderBy('id', 'desc')->value('price');

        $total_e = EDealLog::where('type', 1)->sum('money');
        $deal_line = new Deal_line();
        $deal_line->min = 1;
        $deal_line->max = 1;
        $deal_line->total = 1;
        $deal_line->price = $yesterday_price + 0.01;
        $deal_line->deal_price = $deal_price ? $deal_price : $yesterday->deal_price;
        $deal_line->total_hand = $yesterday->total_hand ? $yesterday->total_hand : 0;
        $deal_line->total_e = $total_e;
        $deal_line->date = date('n.j', strtotime("-1 day"));

        $deal_line->save();
    }
}
